package logic

import (
	"cloud-disk/core/models"
	"context"
	"gorm.io/gorm"

	"cloud-disk/core/internal/svc"
	"cloud-disk/core/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type FileShareDetailLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewFileShareDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FileShareDetailLogic {
	return &FileShareDetailLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *FileShareDetailLogic) FileShareDetail(req *types.FileShareDetailRequest) (resp *types.FileShareDetailResponse, err error) {
	//分享次数加1
	err = l.svcCtx.DbConn.Model(&models.ShareBasic{}).Where("identity=?", req.Identity).Update("check_num", gorm.Expr("check_num + ?", 1)).Error
	if err != nil {
		return
	}
	//获取资源详情
	resp = &types.FileShareDetailResponse{}
	err = l.svcCtx.DbConn.Table("share_basic").
		Select("share_basic.repository_identity,repository_pool.name,repository_pool.ext,repository_pool.size,repository_pool.path").
		Joins("left join repository_pool on share_basic.repository_identity=repository_pool.identity").
		Where("share_basic.identity=?", req.Identity).Scan(&resp).Error
	if err != nil {
		return
	}
	return
}
